import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import mintUi from 'mint-ui'
import Vuex from 'vuex'
import resource from 'vue-resource'
import VuePreview from 'vue-preview'
import router from './router.js'
import '../node_modules/mui/css/mui.css'
import '../node_modules/mui/css/icons-extra.css'
import 'bootstrap/dist/css/bootstrap.css'
import 'mint-ui/lib/style.css'
Vue.use(VueRouter)
Vue.use(mintUi)
Vue.use(Vuex)
Vue.use(resource)
Vue.use(VuePreview)
Vue.http.options.root = 'http://www.liulongbin.top:3005'
Vue.http.options.emulateJSON = true;

var store = new Vuex.Store({
        state: {
            car: JSON.parse(localStorage.getItem('car') || '[]')
        },
        mutations: {
            addCar(state, info) {
                let flag = true
                state.car.forEach(item => {
                    if (item.id == info.id) {
                        item.count = parseInt(info.count) + parseInt(item.count)
                        flag = false
                    }
                })
                if (flag) {
                    state.car.push(info)
                }
                localStorage.setItem('car', JSON.stringify(state.car))
            },
            updateCount(state, arr) {
                state.car.forEach(item => {
                    if (item.id == arr[0]) {
                        item.count = arr[1]
                    }
                })
                localStorage.setItem('car', JSON.stringify(state.car))
            },
            removeFormCar(state, id) {
                state.car.forEach((item, i) => {
                    if (item.id == id) {
                        state.car.splice(i, 1)
                    }
                })
                localStorage.setItem('car', JSON.stringify(state.car))
            },
            updataGoodsSelected(state, info) {
                state.car.some(item => {
                    if (item.id == info.id) {
                        item.selected = info.selected
                    }
                })
                localStorage.setItem('car', JSON.stringify(state.car))
            }
        },
        getters: {
            getAllCount(state) {
                let num = 0;
                state.car.forEach(item => {
                    num += parseInt(item.count)
                })
                return num
            },
            getGoodsCount(state) {
                var o = {}
                state.car.forEach(item => {
                    o[item.id] = item.count
                })
                return o
            },
            getGoodsSelected(state) {
                let obj = {}
                state.car.forEach(item => {
                    obj[item.id] = item.selected
                })
                return obj
            },
            getGoodsCountAndAmount(state) {
                var obj1 = {
                    count: 0,
                    amount: 0
                }
                state.car.forEach(item => {
                    if (item.selected) {
                        obj1.count += item.count
                        obj1.amount += item.price + item.count
                    }
                })
                return obj1
            }
        }
    })
    // 格式化时间
Vue.filter('timeF', function(data) {
    const date = new Date(data),
        y = date.getFullYear(),
        m = date.getMonth().toString().padStart(2, '0'),
        d = date.getDate().toString().padStart(2, '0'),
        h = date.getHours().toString().padStart(2, '0'),
        mm = date.getMinutes().toString().padStart(2, '0'),
        s = date.getSeconds().toString().padStart(2, '0');
    return `${y}-${m}-${d} ${h}:${mm}:${s}`
})
new Vue({
    el: '#app',
    render: h => h(App),
    router,
    store
})